<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2021/12/7
 * Time: 16:41
 */

namespace app\project\service;


use app\common\controller\Common;
use think\Db;
use think\Session;

class StatisticsService
{
    //统计列表
    static function ListService($where,$field,$page,$limit){
        $common = new Common();
        $start = 0;
        if($page!=0){
            $start = ($page-1)*$limit;
        }
        $list = $common->public_select("project_statistics",$where,$field,$start,$limit)->order("sort desc,id desc")->select();
        foreach ($list as $k=>$v){
            $list[$k]['score'] = 0;
        }
        if($page!=0){
            $count = $common->public_select("project_statistics",$where)->count("*"); //总条数
            $black['count'] = $count;
            $black['list'] = $list;
        }else{
            $black = $list;
        }
        return $black;
    }

    //执行添加操作
    static function saveService($data){
        $common = new Common();
        //插入记录
        $id = $common->handle("project_statistics",$data);
        if(empty($data['id'])){
            $log['remark'] = "账户【" . Session::get("user_name") . "】新增统计信息,关联id【".$id."】" ;
        }else{
            $log['remark'] = "账户【" . Session::get("user_name") . "】修改统计信息,关联id【".$data['id']."】" ;
        }
        //记录添加日志
        $common->addLog($log);
    }


    //统计详情
    static function detailService($where,$field=null){
        $common = new Common();
        $detail = $common->public_select("project_statistics",$where,$field)->find();
        if(empty($detail)){
            //查询表结构
            $queryTable = Db::query("show columns from us_project_statistics"); //获取表结构
            //查询表结构
            foreach ($queryTable as $item) {
                $detail[$item['Field']] = "";
            };
        }
        return $detail;
    }

    //获取某个字段
    static function valueService($where){
        $common = new Common();
        $detail = $common->public_select("project_statistics",$where)->find();
        return $detail;
    }

    //删除操作
    static function delService($where){
        $common = new Common();
        $list = $common->public_select("project_statistics",$where)->select();
        $common->public_select("project_statistics",$where)->delete();
        foreach ($list as $k=>$v){
            $log['remark'] = "账户【" . Session::get("user_name") . "】删除统计信息,id【".$v['id']."】" ;
            $common->addLog($log);
        }
    }
}